﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.common;
using rock.DAL;
using rock.UI.custom;
using rock.Model;
using System.Data.SqlClient;
using rock.common;

namespace rock.UI.houseinfo
{
    public partial class UserControlRentCustom : UserControl
    {
        private int CURRENT_PAGE = 1;
        DalRENT_CUSTOM dal = new DalRENT_CUSTOM();

        public UserControlRentCustom()
        {
            InitializeComponent();
            

            //this.userControlCommonListHeader1.selectClick = selectClick;

            this.Dock = System.Windows.Forms.DockStyle.Fill;

            this.userControlCommonListPage1.pageClick = refreshData;
            //this.userControlCommonListPage1.toolStripLabel1.ComboBox.SelectedIndex = 0;
            //this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
     
        }
        int XiaoQuSeqid = 0;
        int DongSeqid = 0;
         
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }

            this.refreshData(this.CURRENT_PAGE);
        }


        private void UserControlRentCustom_Load(object sender, EventArgs e)
        {
            refreshData(1);
            
        }

        private void refreshData(int goPage)
        {
            CURRENT_PAGE = goPage;
            String search = this.textBox1.Text;

             StringBuilder where = new StringBuilder(
                 "(" +
                        " 1=1 " +
               " AND " + " NAME LIKE '%" + search + "%'" +
               
               ")"
              );
            
            List<int> status = new List<int>();
            if (this.checkBoxGeren.Checked)
            {
                status.Add(Constant.CUSTOM_TYPE_GEREN);
            }

            if (this.checkBoxShanghu.Checked)
            {
                status.Add(Constant.CUSTOM_TYPE_SHANGHU);
            }

             
            if (status.Count > 0)
            {
                where.Append(" AND RENT_CUSTOM.TYPE in (  " + StringUtils.list2sqlinIds(status) + ")");
            }

            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }

            PageDataSet ds = dal.GetListByPage(where.ToString(), " NAME ASC  ", CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];
            this.userControlCommonListPage1.Inits(ds.AllRecord  ,this.CURRENT_PAGE);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            buttonUpdate_Click(null, null);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            FormRentCustomUpdate fcv = new FormRentCustomUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
           
            if (dataGridView1.CurrentRow!=null)
            {



                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormRentCustomUpdate fcv = new FormRentCustomUpdate();



                fcv._ModelRENT_CUSTOM = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {

                    //有生效合同的用户不给删除
                    String sql = "SELECT * FROM RENT_CONTRACT WHERE RENT_CUSTOM_SEQID = @RENT_CUSTOM_SEQID AND STATUS =  " + Constant.RENT_CONTRACT_STATUS_SHENGXIAO;
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append(sql);

                    SqlParameter[] parameters = {
					        new SqlParameter("@RENT_CUSTOM_SEQID", SqlDbType.Char,10)			
                        };
                    parameters[0].Value = seqidInt;

                    if (DbHelperSQL.Exists(strSql.ToString(), parameters))
                    {
                        MessageBox.Show("此用户有合同正在执行.不能删除!", "提示");
                        return;
                    }



                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {

                        SqlParamInfo info = dal.Delete(seqidInt);
                        DbHelperSQL.ExecuteSql(info.sql, info.parameters);
                        //刷新数据
                        refreshData(CURRENT_PAGE);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            refreshData(CURRENT_PAGE);
        }

        
        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            buttonUpdate_Click(null, null); 
        }
        
       
         
        private void UserControlHouseRoom_Load(object sender, EventArgs e)
        {
            refreshData(1);
        }


         
        

      
       
        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {
            int cos = e.ColumnIndex;
            if (cos == 14) {
                FormShowCustomRentRoom FSCRR = new FormShowCustomRentRoom();
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                String roomCount = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["ROOM_COUNT"].Value.ToString();

                if (Convert.ToInt32(roomCount) > 0) {
                    ModelRENT_CUSTOM model = this.dal.GetModel(Convert.ToInt32(seqid));
                    FSCRR._ModelRENT_CUSTOM = model;
                    FSCRR.ShowDialog(this);
                }
               
            }       
        }

        /// <summary>
        /// 签合同
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonContract_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                FormRentContractUpdate fcv = new FormRentContractUpdate();
                fcv.oneRefreshParentData = refreshData;
                fcv._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID = Convert.ToInt32(seqid);
                fcv.ShowDialog(this);
                fcv.oneRefreshParentData = refreshData;
              

            }
            else
            {
                MessageBox.Show("请选择要签合同的记录.", "提示");
            }
        }
        //双击更新.
        private void dataGridView1_CellDoubleClick_2(object sender, DataGridViewCellEventArgs e)
        {
            this.buttonUpdate_Click(null, null);
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        
        }

        
    }
}
